﻿/************************************************************************
 * 项目名称 :  Lm.Core.Common.Util  
 * 项目描述 :  扫描器动作帮助类 
 * 类 名 称 :  ScanActionHelper
 * 版 本 号 :  v1.0.0.0 
 * 说    明 :  扫描器动作帮助类  
 * 作    者 :  Lewis
 * 创建时间 :  2022/3/27 10:13:14
 * 更新时间 :  2022/3/27 10:13:14
************************************************************************/

using Modules.Units;
using Modules.Units.Commond;
using SortingWorkerService;
using System;

using System.IO.Ports;
using System.Threading;

namespace Lm.Core.Common.Util
{
    /// <summary>
    /// 扫描器动作帮助类
    /// </summary>
    public static class ScanActionHelper
    {

        #region 私有属性

        /// <summary>
        /// 扫描器串口实例
        /// </summary>
        private readonly static SerialPort serialPort = new SerialPort();
        private static readonly ScanCMD scanCMD = new ScanCMD();

        #endregion

        #region 公开属性

        #endregion

        #region 私有方法

        #endregion


        #region 公有方法

        /// <summary>
        /// 开启扫描器串口
        /// </summary>
        /// <returns></returns>
        public static bool OpenScanSerialPort()
        {
            try
            {
                var res = false;

                serialPort.PortName = AppsettingUnits.Appseeting.ScanCom;
                serialPort.BaudRate = 115200;
                serialPort.DataBits = 8;
                serialPort.StopBits = StopBits.One;
                //serialPort.ReadTimeout = 500;
                //serialPort.WriteTimeout = 500;
                serialPort.Open();
                res = true;

                return res;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        /// <summary>
        /// 串口读取
        /// </summary>
        public static string SerialRead()
        {

          
            try
            {
                if (!serialPort.IsOpen)
                {
                    OpenScanSerialPort();
                }
                byte[] msg = new byte[serialPort.BytesToRead];
                serialPort.Read(msg, 0, serialPort.BytesToRead);
                if (msg.Length > 0)
                {
                    var infoHex = UtilHelper.byteToHexStr(msg);
                    var info = UtilHelper.GetChsFromHex(infoHex);
                    return info;
                }
                else
                {
                    return String.Empty;
                }

            }
            catch (Exception)
            {

                return "";
            }

        }


        /// <summary>
        /// 设置扫描器恢复默认
        /// </summary>
        /// <returns></returns>
        public static bool SetDefault()
        {
            try
            {
                if (!serialPort.IsOpen)
                {
                    OpenScanSerialPort();
                }
                serialPort.Write(scanCMD.RestoreDefault, 0, scanCMD.RestoreDefault.Length);
                byte[] msg = new byte[serialPort.BytesToRead];
                serialPort.Read(msg, 0, serialPort.BytesToRead);
                if (!UtilHelper.ByteArrayCompare(msg, scanCMD.ReturnRestoreDefault))
                    return false;
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 设置手动模式
        /// </summary>
        /// <returns></returns>
        public static string  SetManual()
        {

            if (!serialPort.IsOpen)
            {
                OpenScanSerialPort();
            }
            serialPort.Write(scanCMD.Manual, 0, scanCMD.Manual.Length);
            Thread.Sleep(300);
            byte[] msg = new byte[serialPort.BytesToRead];
            serialPort.Read(msg, 0, serialPort.BytesToRead);
            if (msg.Length > 0)
            {
                var infoHex = UtilHelper.byteToHexStr(msg);
                var info = UtilHelper.GetChsFromHex(infoHex);
                return info;
            }
            else
            {
                return String.Empty;
            }
            //if (!UtilHelper.ByteArrayCompare(msg, scanCMD.ReturnManual))
            //    return false;
            //return true;
        }



        /// <summary>
        /// 设置自动模式（普通）
        /// </summary>
        /// <returns></returns>
        public static bool SetAutoCom()
        {
            if (!serialPort.IsOpen)
            {
                OpenScanSerialPort();
            }
            serialPort.Write(scanCMD.AutoGeneral, 0, scanCMD.AutoGeneral.Length);
            Thread.Sleep(300);
            byte[] msg = new byte[serialPort.BytesToRead];
            serialPort.Read(msg, 0, serialPort.BytesToRead);
            if (!UtilHelper.ByteArrayCompare(msg, scanCMD.ReturnAutoGeneral))
                return false;
            return true;
        }

        /// <summary>
        /// 设置自动模式（快速）
        /// </summary>
        /// <returns></returns>
        public static string SetAutoFast()
        {
            if (!serialPort.IsOpen)
            {
                OpenScanSerialPort();
            }
            serialPort.Write(scanCMD.AutoFast, 0, scanCMD.AutoFast.Length);
            Thread.Sleep(300);
            byte[] msg = new byte[serialPort.BytesToRead];
            serialPort.Read(msg, 0, serialPort.BytesToRead);
            //if (!UtilHelper.ByteArrayCompare(msg, scanCMD.ReturnAutoFast))
                if (msg.Length > 0)
                {
                    var infoHex = UtilHelper.byteToHexStr(msg);
                    var info = UtilHelper.GetChsFromHex(infoHex);
                    return info;
                }
                else
                {
                    return String.Empty;
                }

         
        }


        /// <summary>
        /// 设置扫描超时永不关闭
        /// </summary>
        /// <returns></returns>
        public static bool SetScanTimeOutNerverClose()
        {
            if (!serialPort.IsOpen)
            {
                OpenScanSerialPort();
            }
            serialPort.Write(scanCMD.ScanTimeOutNerverClose, 0, scanCMD.ScanTimeOutNerverClose.Length);
            Thread.Sleep(300);
            byte[] msg = new byte[serialPort.BytesToRead];
            serialPort.Read(msg, 0, serialPort.BytesToRead);
            if (!UtilHelper.ByteArrayCompare(msg, scanCMD.ReturnScanTimeOutNerverClose))
                return false;
            return true;
        }


        /// <summary>
        /// 开启扫描超时提示
        /// </summary>
        /// <returns></returns>
        public static bool SetOpenScanTimeOutHint()
        {
            if (!serialPort.IsOpen)
            {
                OpenScanSerialPort();
            }
            serialPort.Write(scanCMD.OpenScanTimeOutHint, 0, scanCMD.OpenScanTimeOutHint.Length);
            Thread.Sleep(300);
            byte[] msg = new byte[serialPort.BytesToRead];
            serialPort.Read(msg, 0, serialPort.BytesToRead);
            if (!UtilHelper.ByteArrayCompare(msg, scanCMD.ReturnOpenScanTimeOutHint))
                return false;
            return true;
        }


        /// <summary>
        /// 关闭扫描超时提示
        /// </summary>
        /// <returns></returns>
        public static bool SetCloseScanTimeOutHint()
        {
            if (!serialPort.IsOpen)
            {
                OpenScanSerialPort();
            }
            serialPort.Write(scanCMD.CloseScanTimeOutHint, 0, scanCMD.CloseScanTimeOutHint.Length);
            Thread.Sleep(300);
            byte[] msg = new byte[serialPort.BytesToRead];
            serialPort.Read(msg, 0, serialPort.BytesToRead);
            if (!UtilHelper.ByteArrayCompare(msg, scanCMD.ReturnCloseScanTimeOutHint))
                return false;
            return true;
        }

        /// <summary>
        /// 添加后缀回车
        /// </summary>
        /// <returns></returns>
        public static bool SetAddSuffixAndEnter()
        {
            if (!serialPort.IsOpen)
            {
                OpenScanSerialPort();
            }
            serialPort.Write(scanCMD.AddSuffixAndEnter, 0, scanCMD.AddSuffixAndEnter.Length);
            Thread.Sleep(300);
            byte[] msg = new byte[serialPort.BytesToRead];
            serialPort.Read(msg, 0, serialPort.BytesToRead);
            if (!UtilHelper.ByteArrayCompare(msg, scanCMD.ReturnAddSuffixAndEnter))
                return false;
            return true;
        }


        /// <summary>
        /// 存活检测
        /// </summary>
        /// <returns></returns>
        public static bool SetSurvive()
        {
            //    if (!serialPort.IsOpen)
            //    {
            //        OpenScanSerialPort();
            //    }
            //    serialPort.Write(scanCMD.Survive, 0, scanCMD.Survive.Length);
            //    Thread.Sleep(300);
            //    byte[] msg = new byte[serialPort.BytesToRead];
            //    serialPort.Read(msg, 0, serialPort.BytesToRead);
            //    if (!UtilHelper.ByteArrayCompare(msg, scanCMD.ReturnSurvive))
            //        return false;
            return true;
        }


        /// <summary>
        /// 重启扫描器
        /// </summary>
        /// <returns></returns>
        public static bool SetResetScan()
        {
            if (!serialPort.IsOpen)
            {
                OpenScanSerialPort();
            }
            serialPort.Write(scanCMD.ResetScan, 0, scanCMD.ResetScan.Length);
            return true;
        }



        /// <summary>
        /// 获取软件版本
        /// </summary>
        /// <returns></returns>
        public static dynamic GetSoftWareVersion()
        {
            if (!serialPort.IsOpen)
            {
                OpenScanSerialPort();
            }
            serialPort.Write(scanCMD.ReadSoftWareVersion, 0, scanCMD.ReadSoftWareVersion.Length);
            Thread.Sleep(300);
            byte[] msg = new byte[serialPort.BytesToRead];
            serialPort.Read(msg, 0, serialPort.BytesToRead);
            var version = msg[6];

            return 0;
        }

        /// <summary>
        /// 执行扫描（手动）
        /// </summary>
        /// <returns></returns>
        public static dynamic RunScan()
        {
            if (!serialPort.IsOpen)
            {
                OpenScanSerialPort();
            }
            serialPort.Write(scanCMD.RunScan, 0, scanCMD.RunScan.Length);
            byte[] msg = new byte[serialPort.BytesToRead];
            while (true)
            {
                Thread.Sleep(300);
                msg = new byte[serialPort.BytesToRead];
                serialPort.Read(msg, 0, serialPort.BytesToRead);

                if (msg.Length > 0)
                {
                    break;
                }
            }

            var infoHex = UtilHelper.byteToHexStr(msg);
            var info = UtilHelper.GetChsFromHex(infoHex);

            return info;
        }


        #endregion


        #region 实现方法

        #endregion


    }
}
